package com.mes.cloud.service;

import com.mes.cloud.api.dto.request.SysGlobalLogConfigureAddRequest;
import com.mes.cloud.api.dto.request.SysGlobalLogConfigureRequest;
import com.mes.cloud.api.dto.request.SysGlobalLogConfigureUpdateRequest;
import com.mes.cloud.api.dto.response.SysGlobalLogConfigureDto;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.SysGlobalLogConfigureDao;
import com.mes.cloud.entity.SysGlobalLogConfigure;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 全局日志配置表
 *
 * @author xy
 * @version 2019-11-13 09:53:12
 * @email yuan.xue3@pactera.com
 */
@Service
public class SysGlobalLogConfigureService {

    private SysGlobalLogConfigureDao sysGlobalLogConfigureDao;

    private IPKeyGenerator ipKeyGenerator;

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    public SysGlobalLogConfigureService(SysGlobalLogConfigureDao sysGlobalLogConfigureDao, IPKeyGenerator ipKeyGenerator) {
        this.sysGlobalLogConfigureDao = sysGlobalLogConfigureDao;
        this.ipKeyGenerator = ipKeyGenerator;
    }

    /**
     * 查列表
     *
     * @param sysGlobalLogConfigureRequest
     * @return JsonResult
     */
    public JsonResult<List<SysGlobalLogConfigureDto>> list(SysGlobalLogConfigureRequest sysGlobalLogConfigureRequest) {
        //查询条件
        SysGlobalLogConfigure selectParam = new SysGlobalLogConfigure();
        BeanUtils.copyProperties(sysGlobalLogConfigureRequest, selectParam);
        selectParam.setDelFlag(Constants.DEL_FLAG_NO);
        List<SysGlobalLogConfigure> sysGlobalLogConfigureList = sysGlobalLogConfigureDao.list(selectParam);
        List<SysGlobalLogConfigureDto> sysGlobalLogConfigureDtoList = new ArrayList<>();
        for (SysGlobalLogConfigure sysGlobalLogConfigure : sysGlobalLogConfigureList) {
            SysGlobalLogConfigureDto sysGlobalLogConfigureDto = new SysGlobalLogConfigureDto();
            BeanUtils.copyProperties(sysGlobalLogConfigure, sysGlobalLogConfigureDto);
            sysGlobalLogConfigureDtoList.add(sysGlobalLogConfigureDto);
        }
        JsonResult<List<SysGlobalLogConfigureDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(sysGlobalLogConfigureDtoList);
        return jsonResult;
    }

    /**
     * 新增
     *
     * @param sysGlobalLogConfigureAddRequest
     * @return JsonResult
     */
    public JsonResult<String> add(SysGlobalLogConfigureAddRequest sysGlobalLogConfigureAddRequest) {
        JsonResult<String> jsonResult = new JsonResult<>();
        String url = sysGlobalLogConfigureAddRequest.getRequestUrl();

        SysGlobalLogConfigure queryParams = new SysGlobalLogConfigure();
        queryParams.setRequestUrl(url);
        int sysGlobalLogConfigureCount = sysGlobalLogConfigureDao.selectCount(queryParams);
        //已存在相同url
        if (sysGlobalLogConfigureCount > 0) {
            jsonResult.setMsg(HttpStatusEnum.RECORD_EXIST.getMessage()).setCode(HttpStatusEnum.RECORD_EXIST.getCode());
            return jsonResult;
        }

        SysGlobalLogConfigure sysGlobalLogConfigure = new SysGlobalLogConfigure();
        BeanUtils.copyProperties(sysGlobalLogConfigureAddRequest, sysGlobalLogConfigure);
        String id = ipKeyGenerator.getGenerateKey();
        sysGlobalLogConfigure.setId(id);
        sysGlobalLogConfigure.setUpdateDateStr(DateUtil.updateDateStr());
        sysGlobalLogConfigure.setCreateBy(UserUtil.getEmployeeNoAndName());
        sysGlobalLogConfigure.setCreateByNo(UserUtil.getUpdateByNo());
        sysGlobalLogConfigure.setUpdateBy(UserUtil.getEmployeeNoAndName());
        sysGlobalLogConfigure.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = sysGlobalLogConfigureDao.insertSelective(sysGlobalLogConfigure);
        if (count > 0) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
        } else {
            jsonResult.setMsg(HttpStatusEnum.FAIL.getMessage()).setCode(HttpStatusEnum.FAIL.getCode());
        }
        jsonResult.setData(id);
        return jsonResult;
    }

    /**
     * 更新
     *
     * @param sysGlobalLogConfigureUpdateRequest
     * @return JsonResult
     */
    public JsonResult update(SysGlobalLogConfigureUpdateRequest sysGlobalLogConfigureUpdateRequest) {
        JsonResult jsonResult = new JsonResult();

        String url = sysGlobalLogConfigureUpdateRequest.getRequestUrl();
        //通过url查询数据
        SysGlobalLogConfigure queryParams = new SysGlobalLogConfigure();
        queryParams.setRequestUrl(url);
        queryParams = sysGlobalLogConfigureDao.selectOne(queryParams);
        if (!Objects.isNull(queryParams)) {
            //通过url查询的数据 id不是当前提交的数据id
            if(!queryParams.getId().equals(sysGlobalLogConfigureUpdateRequest.getId())){
                jsonResult.setMsg(HttpStatusEnum.RECORD_EXIST.getMessage()).setCode(HttpStatusEnum.RECORD_EXIST.getCode());
                return jsonResult;
            }
        }

        SysGlobalLogConfigure sysGlobalLogConfigure = new SysGlobalLogConfigure();
        BeanUtils.copyProperties(sysGlobalLogConfigureUpdateRequest, sysGlobalLogConfigure);
        sysGlobalLogConfigure.setUpdateDateStr(DateUtil.updateDateStr());
        sysGlobalLogConfigure.setUpdateByNo(UserUtil.getUpdateByNo());
        sysGlobalLogConfigure.setUpdateBy(UserUtil.getEmployeeNoAndName());
        int count = sysGlobalLogConfigureDao.updateByPrimaryKeySelective(sysGlobalLogConfigure);
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            jsonResult.setMsg(HttpStatusEnum.FAIL.getMessage());
            jsonResult.setCode(HttpStatusEnum.FAIL.getCode());
        }
        return jsonResult;
    }

    /**
     * 删除
     *
     * @param id
     * @return JsonResult
     */
    public JsonResult delete(String id) {
        SysGlobalLogConfigure sysGlobalLogConfigure = new SysGlobalLogConfigure();
        sysGlobalLogConfigure.setId(id);
        sysGlobalLogConfigure.setDelFlag(Constants.DEL_FLAG_YES);
        sysGlobalLogConfigure.setUpdateDateStr(DateUtil.updateDateStr());
        sysGlobalLogConfigure.setUpdateByNo(UserUtil.getUpdateByNo());
        sysGlobalLogConfigure.setUpdateBy(UserUtil.getEmployeeNoAndName());
        int count = sysGlobalLogConfigureDao.updateByPrimaryKeySelective(sysGlobalLogConfigure);
        JsonResult jsonResult = new JsonResult();
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            jsonResult.setMsg(HttpStatusEnum.FAIL.getMessage());
            jsonResult.setCode(HttpStatusEnum.FAIL.getCode());
        }
        return jsonResult;
    }

    /**
     * 查看详情
     *
     * @param id
     * @return JsonResult
     */
    public JsonResult<SysGlobalLogConfigureDto> get(String id) {
        JsonResult<SysGlobalLogConfigureDto> jsonResult = new JsonResult<>();
        SysGlobalLogConfigure sysGlobalLogConfigure = sysGlobalLogConfigureDao.selectByPrimaryKey(id);
        if (sysGlobalLogConfigure == null) {
            return jsonResult.setCode(HttpStatusEnum.RECORD_NOT_EXIST.getCode()).setMsg(HttpStatusEnum.RECORD_NOT_EXIST.getMessage());
        }
        SysGlobalLogConfigureDto sysGlobalLogConfigureDto = new SysGlobalLogConfigureDto();
        BeanUtils.copyProperties(sysGlobalLogConfigure, sysGlobalLogConfigureDto);
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(sysGlobalLogConfigureDto);
    }

    public JsonResult<SysGlobalLogConfigureDto> getSysGlobalLogConfigure(SysGlobalLogConfigureRequest sysGlobalLogConfigureRequest) {
        JsonResult<SysGlobalLogConfigureDto> jsonResult = new JsonResult<>();
        SysGlobalLogConfigure sysGlobalLogConfigure = new SysGlobalLogConfigure();

        BeanUtils.copyProperties(sysGlobalLogConfigureRequest, sysGlobalLogConfigure);
        List<SysGlobalLogConfigure>  list = sysGlobalLogConfigureDao.select(sysGlobalLogConfigure);
        if (CollectionUtils.isEmpty(list)) {
            return jsonResult.setCode(HttpStatusEnum.RECORD_NOT_EXIST.getCode()).setMsg(HttpStatusEnum.RECORD_NOT_EXIST.getMessage());
        }else{
            sysGlobalLogConfigure = list.get(0);
        }
        SysGlobalLogConfigureDto sysGlobalLogConfigureDto = new SysGlobalLogConfigureDto();
        BeanUtils.copyProperties(sysGlobalLogConfigure, sysGlobalLogConfigureDto);
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(sysGlobalLogConfigureDto);
    }
}
